package com.example.fw;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.tests.ContactData;
import com.example.utils.SortedListOf;

public class ContactHelper extends HelperBase {
	
	public static boolean CREATION = true;
	public static boolean MODIFICATION = true;

	private SortedListOf<ContactData> cachedContacts;
	
	public SortedListOf<ContactData> getContacts() {
		
		if (cachedContacts == null) {
			rebuildContactCach();
		}
		return cachedContacts;

	}	
	
	private void rebuildContactCach() {
		cachedContacts = new SortedListOf<ContactData>();
		manager.navigateTo().mainPage();
		List<WebElement> contactRows = driver.findElements(By.xpath("//tr[@name='entry']/td[2]"));
		for (WebElement contactRow : contactRows) {
			String lastName = contactRow.getText();
			cachedContacts.add(new ContactData().withlastName(lastName)) ;
	
		}
	}

	public ContactHelper createContact(ContactData contact) {
		manager.navigateTo().mainPage();
		initContactCreation();
		fillContactForm(contact, CREATION);
		submitNewContact();
		returtToHomePage();
		rebuildContactCach();
		return this;
	}
	
	public ContactHelper modifyContact(ContactData contact, int index1) {
		initContactModif(index1);
		fillContactForm(contact, MODIFICATION);
		submitContactModif();
		returtToHomePage();
		rebuildContactCach();
		return this;
	}
	
	public ContactHelper deleteContact(int index1) {
		selectContactByIndex(index1);
		submitContactDeletion();
		returtToHomePage();
		rebuildContactCach();
		return this;
	}
	
//---------------------------------------------------------------------------------------	
	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}

	public ContactHelper submitNewContact() {
		click(By.name("submit"));
		cachedContacts = null; 
		return this;
	}

	public ContactHelper initContactCreation() {
		click(By.linkText("add new"));
		return this;
	}
	

	public ContactHelper initContactModif(int index1) {
		selectContactByIndex(index1);
		return this;
	}
	
	private ContactHelper selectContactByIndex(int index1) {
		click(By.xpath("//tr["+ (index1+2) + "]/td[7]"));
		return this;
	}

	public ContactHelper fillContactForm(ContactData contact, boolean formType) {
		type(By.name("firstname"), contact.getfirstName());
		type(By.name("lastname"), contact.getlastName());
		type(By.name("address"), contact.getaddress());
		type(By.name("home"), contact.gethomeNumber());
		type(By.name("mobile"), contact.getmobileNumber());
		type(By.name("work"), contact.getworkNumber());
		type(By.name("email"), contact.getmail());
		type(By.name("email2"), contact.getmail2());
		type(By.name("byear"), contact.getbirthYear());
		type(By.name("address2"), contact.getsecAddress());
		type(By.name("phone2"), contact.getsecHomeNumber());
	

		selectByText(By.name("bday"), contact.getbirthDay());
		selectByText(By.name("bmonth"), contact.getbirthMonth());
		
		if (formType == CREATION) {
		//selectByText(By.name("new_group"), contact.group);
		}
		else {
			if (driver.findElements(By.name("new_group")).size() !=0) {
				throw new Error("Group Selector exist in Contact modification form");
			}
		}
		return this;
	}


	public ContactHelper returtToHomePage() {
		click(By.linkText("home page"));
		return this;
	}

	private ContactHelper submitContactDeletion() {
		click(By.xpath("//form[2]/input[@name='update']"));
		cachedContacts = null; 
		return this;
	}
	
	
	public ContactHelper submitContactModif() {
		click(By.xpath("//form[1]/input[@name='update']"));
		cachedContacts = null; 
		return this;
		
	}


}
